package se.cefalo.ceti.ical;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import java.io.StringReader;

/**
 * Test a url.
 */
public class TestICalUrlController extends SimpleFormController {


    public TestICalUrlController() {
        setCommandName("IcalUrl");
        setCommandClass(IcalUrl.class);


    }


    protected ModelAndView onSubmit(Object command, BindException errors) throws Exception {

        IcalUrl myCommand = (IcalUrl) command;

        String icalDB = urlRetrieverService.getContent(myCommand.getIcalUrl());

        iCalService.setIcalDB(new StringReader(icalDB));


        ModelAndView modelAndView = super.onSubmit(command, errors);
        modelAndView.addObject(iCalService);
        return modelAndView;
    }


    public UrlRetrieverService getUrlRetrieverService() {
        return urlRetrieverService;
    }

    public void setUrlRetrieverService(UrlRetrieverService urlRetrieverService) {
        this.urlRetrieverService = urlRetrieverService;
    }


    public ICalService getiCalService() {
        return iCalService;
    }

    public void setiCalService(ICalService iCalService) {
        this.iCalService = iCalService;
    }

    ICalService iCalService;

    UrlRetrieverService urlRetrieverService;

}
